<?php
namespace app\admin\controller;

use app\admin\services\MemberService;
use app\common\controller\Base;
use app\common\model\Admin;
use app\common\services\LoginService;

class Member extends Base
{
    /**
     * @var MemberService
     */
    protected $_service;

    protected function _setService()
    {
        $this->_service = new MemberService();
    }

    public function pwd()
    {
        if (IS_POST) {
            $old = I('post.oldpwd', '', 'trim');
            $pwd = I('post.pwd', '', 'trim');
            $pwd2 = I('post.pwd2', '', 'trim');
            if ($pwd != $pwd2) {
                return $this->jsonFail('新密码和确认密码不一致');
            }
            $oldPwd = Admin::instance()->where(['id' => LoginService::getLoginUserId()])->getField('password');
            if ($oldPwd != ccnn_md5($old)) {
                return $this->jsonFail('旧密码错误');
            }
            Admin::instance()->where(['id' => LoginService::getLoginUserId()])->save([
                'password' => ccnn_md5($pwd)
            ]);
            return $this->jsonSuccess('修改成功');
        }
        return $this->fetch();
    }

    public function info()
    {
        $info = Admin::instance()->where(['id' => LoginService::getLoginUserId()])->find();
        unset($info['password']);
        $this->assign(['m' => $info]);
        return $this->fetch();
    }
}